
package com.hyst.web;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.hyst.common.ResultBean;

/**
 * @Description:
 * @Date：2018年9月6日 @author： DongY
 * @version ：1.0 1.[2018年9月6日]CreatBy：dy
 */
@RestController
@RequestMapping("upload")
public class UploadController {

	/**
	 * 上传教材封面图片
	 * 
	 * @throws IOException
	 * @throws IllegalStateException
	 */
	@PostMapping("cover")
	public ResultBean<String> cover(final MultipartFile file, final HttpServletRequest request)
			throws IllegalStateException, IOException {
		final long timestampe = System.currentTimeMillis();
		final String name = file.getOriginalFilename();
		final String fileName = "/cover/" + timestampe + "_" + name.substring(name.lastIndexOf("."));
		final String basePath = request.getSession().getServletContext().getRealPath("/");
		final File dir = new File(basePath + "/cover/");
		if (!dir.exists()) {
			dir.mkdirs();
		}
		final String realeName = basePath + fileName;
		file.transferTo(new File(realeName));
		return ResultBean.success(fileName);
	}
}
